package com.gs.api.core.common.oss;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author guoyunfei on 2021/8/29 13:49
 * @className FilesService
 * @description TODO
 * @Version 1.0
 */
@Service
public class FilesService {

    @Resource
    private Qiniu qiniu;

    /**
     * 获取访问路径(http或者https)
     * @return
     */
    public String ossResourceUrl(){
        return qiniu.getResourcesUrl();
    }

    public com.qiniu.storage.Configuration qiniuZone(String zoneStr) {
        Zone zone = null;
        if (Objects.equals(zoneStr, QiniuZone.HUA_BEI)) {
            zone = Zone.huabei();
        } else if (Objects.equals(zoneStr, QiniuZone.HUA_DONG)) {
            zone = Zone.huadong();
        } else if (Objects.equals(zoneStr, QiniuZone.HUA_NAN)) {
            zone = Zone.huanan();
        } else if (Objects.equals(zoneStr, QiniuZone.BEI_MEI)) {
            zone = Zone.beimei();
        } else if (Objects.equals(zoneStr, QiniuZone.XIN_JIA_PO)) {
            zone = Zone.xinjiapo();
        }
        return new com.qiniu.storage.Configuration(zone);
    }

    public Zone getQiniuZone(String zoneStr) {
        Configuration configuration = qiniuZone(zoneStr);
        return configuration.zone;
    }

    /**
     * 上传文件(七牛云)
     * @param file
     * @return
     * @throws QiniuException
     */
    public String uploadFileQiniu(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String extName = FileUtil.extName(filename);
        String rFileName = IdUtil.simpleUUID() + "." + extName;
        //存储东南亚
        Configuration cfg = new Configuration(getQiniuZone(qiniu.getZone()));
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(qiniu.getAccessKey(), qiniu.getSecretKey());
        String upToken = auth.uploadToken(qiniu.getBucket(), rFileName);
        Response response = uploadManager.put(file.getBytes(), rFileName, upToken);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        System.out.println(putRet);
        return rFileName;
    }

    public String uploadFileQiniu(File file) throws IOException {
        String rFileName = IdUtil.simpleUUID() + ".png";
        //存储东南亚
        Configuration cfg = new Configuration(getQiniuZone(qiniu.getZone()));
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(qiniu.getAccessKey(), qiniu.getSecretKey());
        String upToken = auth.uploadToken(qiniu.getBucket(), rFileName);
        Response response = uploadManager.put(file, rFileName, upToken);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        System.out.println(putRet);
        return rFileName;
    }

}
